/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scm.inventoryControl.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.SalesmanSessionBean;
import commonInfrastructure.internalMessaging.session.MessageSessionBean;
import commonInfrastructure.system.session.SystemSessionBean;
import crm.salesOrderProcessing.entity.ProductEntity;
import crm.salesOrderProcessing.session.ProductManageSessionBean;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import scm.inventoryControl.entity.ProductBatchRecordEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class CheckPromotionManagedBean {

    @EJB
    private ProductManageSessionBean productSession;
    private List<ProductEntity> productList;
    @EJB
    private InventoryControlSessionBean inventorySession;
    @EJB
    SystemSessionBean systemSession = new SystemSessionBean();
    @EJB
    MessageSessionBean messageManager;
    @EJB
    SalesmanSessionBean salesman;

    /** Creates a new instance of ManagePromotionManagedBean */
    public CheckPromotionManagedBean() {
        productSession = new ProductManageSessionBean();
        inventorySession = new InventoryControlSessionBean();
        messageManager = new MessageSessionBean();
        salesman = new SalesmanSessionBean();
    }

    public void initView(PhaseEvent event) {

        productList = productSession.getAllProducts();

    }

    public List<ProductEntity> getProductList() {
        return productList;
    }

    public void update(ActionEvent action) throws ParseException {
        List<ProductBatchRecordEntity> recordList = inventorySession.getAllProductRecords();
        int quantity = 0;
        Date expire;
        Date promote;
        for (int p = 0; p < recordList.size(); p++) {
            Calendar c1 = Calendar.getInstance();
            c1.setTime(recordList.get(p).getManufactureDate());
            System.out.println("Date is : " + c1.getTime());
            c1.add(Calendar.MONTH, 3);
            System.out.println("Date + 3 month is : " + c1.getTime());
            promote = c1.getTime();
            c1.add(Calendar.MONTH, 1);
            expire = c1.getTime();
            System.out.println("Date + 4 month is : " + c1.getTime());
            if (systemSession.getToday().after(expire)) {
                inventorySession.deleteRecord(recordList.get(p));
            } else if (systemSession.getToday().after(promote)) {
                for (int q = 0; q < productList.size(); q++) {
                    if (productList.get(q).getProductName().equals(recordList.get(p).getProductInventoryLevel().getProduct().getProductName())) {
                        quantity = recordList.get(p).getQuantity();
                        productList.get(q).setPromotionQuantity(quantity);
                        break;
                    }
                }
            }
        }

        for (int i = 0; i < productList.size(); i++) {
            productSession.saveChanges(productList.get(i));
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Quantity to Promote Updated Successfully", ""));
    }

    public void send(ActionEvent event) {
        try {
            List<SalesmanEntity> salesList = salesman.getAllSalesman();
            SalesmanEntity salesManager = null;
            for (int i = 0; i < salesList.size(); i++) {
                if (salesList.get(i).getSalesManLevel().equals("4")) {
                    salesManager = salesList.get(i);
                    break;
                }
            }
            Long receiverId = salesManager.getId();
            String type = "Private";
            Long senderId = systemSession.getCurrentUser().getId();
            String title = "Promotion Needed";
            String msg = "Hi, there are some expiring products need to be promoted. \nPlease check. \n\nThanks,\nWarehouse Department";
            messageManager.addPrivateMessage(senderId, receiverId, title, msg, type);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Send Successfully", ""));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "No such person", ""));
            return;
        }

    }
}
